﻿using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Web;
using SmartSapce.ServiceLocation;

namespace SmartSpace.WebApp._console.page
{
    public partial class page_navigation_edit : System.Web.UI.Page
    {
        public Guid _pageId;
        public Guid _id;

        protected void Page_Load(object sender, EventArgs e)
        {
            _pageId = Request.GetGuid("id");
            _id = Request.GetGuid("selid");

            _ctlLinkBack.NavigateUrl = string.Format("./page-navigation-list.aspx?id={0}", _pageId);

            LoadData();
        }

        private void LoadData()
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            Navigation navigation = service.GetNavigation(_id);
            if (navigation != null) 
            {
                _ctlEditName.Text = navigation.Code;
                _ctlEditArguments.Text = navigation.Arguments;
                _ctlEditCaption.Text = navigation.Caption;
                _ctlEditDescription.Text = navigation.Description;
                _ctlEditGroupName.Text = navigation.ContainerCode;
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            Navigation navigation = service.GetNavigation(_id);
            navigation.Code = _ctlEditName.Text;

            if (!string.IsNullOrEmpty(_ctlEditImage.Text))
            {
                navigation.ImageId = new Guid(_ctlEditImage.Text);
            }

            navigation.Caption = _ctlEditCaption.Text;
            navigation.Description = _ctlEditDescription.Text;
            navigation.ContainerCode = _ctlEditGroupName.Text;
            navigation.Arguments = _ctlEditArguments.Text;

            if (radio_other.Checked)
            {
                navigation.TargetType = NavigationTargetType.Page;
                navigation.Url = _ctlEditUrl.Text;
            }
            else
            {
                //if (_ctlTreeView.CheckedNodes.Count > 0)
                //{
                //    TreeNode selectedViewNode = _ctlTreeView.CheckedNodes[0];
                //    TreeNode selectedModuleNode = selectedViewNode.Parent;
                //    //menu.ModuleId = new Guid(selectedModuleNode.Value);
                //    menu.Url = string.Format("v:>>{0}", selectedViewNode.Value);
                //}
            }

            service.SaveNavigation(navigation);
            Response.Redirect(_ctlLinkBack.NavigateUrl);
        }

        protected void _ctlReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("./page-navigation-edit.aspx?id={0}", _pageId));
        }
    }
}